package com.lcp.controller;

import com.lcp.common.Result;
import com.lcp.entity.HealthRecord;
import com.lcp.entity.User;
import com.lcp.service.HealthRecordService;
import com.lcp.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/health-records")
public class HealthRecordController {

    private static final Logger logger = LoggerFactory.getLogger(HealthRecordController.class);

    @Autowired
    private HealthRecordService healthRecordService;

    @Autowired
    private UserService userService;

    @GetMapping("/current")
    public Result<HealthRecord> getCurrentUserHealthRecord() {
        logger.info("getCurrentUserHealthRecord method is called");
        User currentUser = userService.getCurrentUser();
        logger.info("Current user ID: {}", currentUser.getId());
        HealthRecord healthRecord = healthRecordService.getByUserId(currentUser.getId());
        logger.info("Health record for current user: {}", healthRecord);
        return Result.success(healthRecord);
    }

    @GetMapping("/history")
    public Result<List<HealthRecord>> getHealthHistory() {
        logger.info("getHealthHistory method is called");
        User currentUser = userService.getCurrentUser();
        logger.info("Current user ID: {}", currentUser.getId());
        List<HealthRecord> healthRecords = healthRecordService.listByUserId(currentUser.getId());
        logger.info("Health records for current user: {}", healthRecords);
        return Result.success(healthRecords);
    }

    @PostMapping
    public Result<HealthRecord> create(@RequestBody HealthRecord healthRecord) {
        logger.info("create method is called with healthRecord: {}", healthRecord);
        User currentUser = userService.getCurrentUser();
        healthRecord.setUserId(currentUser.getId());
        HealthRecord createdHealthRecord = healthRecordService.create(healthRecord);
        logger.info("Created health record: {}", createdHealthRecord);
        return Result.success(createdHealthRecord);
    }

    @PutMapping("/{id}")
    public Result<HealthRecord> update(@PathVariable Long id, @RequestBody HealthRecord healthRecord) {
        logger.info("update method is called for id: {}", id);
        healthRecord.setId(id);
        HealthRecord updatedHealthRecord = healthRecordService.update(healthRecord);
        logger.info("Updated health record: {}", updatedHealthRecord);
        return Result.success(updatedHealthRecord);
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        logger.info("delete method is called for id: {}", id);
        healthRecordService.delete(id);
        logger.info("Deleted health record with id: {}", id);
        return Result.success();
    }

    @GetMapping("/user/{userId}")
    public Result<List<HealthRecord>> listByUserId(@PathVariable Long userId) {
        logger.info("listByUserId method is called for userId: {}", userId);
        List<HealthRecord> healthRecords = healthRecordService.listByUserId(userId);
        logger.info("Health records for userId: {} are: {}", userId, healthRecords);
        return Result.success(healthRecords);
    }

    @GetMapping("/{id}")
    public Result<HealthRecord> getHealthRecordById(@PathVariable Long id) {
        logger.info("getHealthRecordById method is called for id: {}", id);
        HealthRecord healthRecord = healthRecordService.getById(id);
        logger.info("Health record for id: {} is: {}", id, healthRecord);
        return Result.success(healthRecord);
    }
}
